package com.ecreditpal.toto;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.ecreditpal.toto.util.ConfigurationManager;
import org.springframework.boot.Banner;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.http.MediaType;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lifeng
 * @version 1.0 on 2018/4/21.
 */
@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan(basePackages = "com.ecreditpal.toto")
public class Application implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
    public static void main(String[] args) {
        new SpringApplicationBuilder().sources(Application.class)
                .bannerMode(Banner.Mode.CONSOLE)
                .run(args);

    }


    @Bean

    public HttpMessageConverters fastJsonHttpMessageConverters(ApplicationContext ctx) {

        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

        FastJsonConfig fastJsonConfig = new FastJsonConfig();

        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);

        fastConverter.setFastJsonConfig(fastJsonConfig);
        List<MediaType> list = new ArrayList<>();
        list.add(MediaType.APPLICATION_JSON_UTF8);
        fastConverter.setSupportedMediaTypes(list);

        return new HttpMessageConverters(fastConverter);

    }


    @Override
    public void customize(ConfigurableServletWebServerFactory configurableServletWebServerFactory) {
        Integer port = ConfigurationManager.getConfiguration().getInt("server.port");
        configurableServletWebServerFactory.setPort(port);
    }
}
